﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace _1stVATCWin
{
    public partial class BaoHanh : Form
    {
        public string username;        

        public BaoHanh()
        {
            InitializeComponent();            
        }

        private void BaoHanh_Load(object sender, EventArgs e)
        {
            Load_DSBH();
        }

        private void Load_DSBH()
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionstring = file.ReadValue("server", "conn");
            //string connectionString = @"Data Source=FOX-PC\SQLEXPRESS;Initial Catalog=Project;Integrated Security=True";
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from BAOHANH";
            SqlCommand cmd = new SqlCommand(sql, conn);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds, "Table BaoHanh");
            DataTable dt = ds.Tables["Table BaoHanh"];

            DSBH.DataSource = ds.Tables["Table BaoHanh"].DefaultView;            
        }

        private void Add_Click(object sender, EventArgs e)
        {
            BH_ADD f = new BH_ADD();
            if (f.ShowDialog() == DialogResult.OK)
                Load_DSBH();
        }

        private void Update_Click(object sender, EventArgs e)
        {
            int i = DSBH.SelectedRows[0].Index;
            int id = int.Parse(DSBH.Rows[i].Cells["IDbaohanh"].Value.ToString());

            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionstring = file.ReadValue("server", "conn");
            //string connectionString = @"Data Source=FOX-PC\SQLEXPRESS;Initial Catalog=Project;Integrated Security=True";
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "update BAOHANH set tenkhachhang = @name,sodt = @dt,serinumber = @seri,tinhtrang = @tt where IDbaohanh = @id";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@id", id);
            cmd.Parameters.AddWithValue("@name", namebh.Text);
            cmd.Parameters.AddWithValue("@dt", sodtbh.Text);
            cmd.Parameters.AddWithValue("@seri", seribh.Text);
            cmd.Parameters.AddWithValue("@tt", tinhtrangbh.Text);

            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

            Load_DSBH();
        }

        private void Del_Click(object sender, EventArgs e)
        {
            if ((MessageBox.Show("Bạn có muốn xóa dữ liệu không?", "Thông Báo", MessageBoxButtons.OKCancel)) == DialogResult.OK)
            {
                int i = DSBH.SelectedRows[0].Index;
                int id = int.Parse(DSBH.Rows[i].Cells["IDbaohanh"].Value.ToString());

                Config file = new Config(Application.StartupPath + "\\config.ini");
                string connectionstring = file.ReadValue("server", "conn");
                //string connectionString = @"Data Source=FOX-PC\SQLEXPRESS;Initial Catalog=Project;Integrated Security=True";
                SqlConnection conn = new SqlConnection(connectionstring);
                string sql = "delete from BAOHANH where IDbaohanh = @id";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@id", id);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

                DSBH.Rows.RemoveAt(i);
                namebh.Text = "";
                sodtbh.Text = "";
                seribh.Text = "";
                tinhtrangbh.Text = "";
            }
        }
       
        private void DSBH_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int id = int.Parse(DSBH.Rows[e.RowIndex].Cells["IDbaohanh"].Value.ToString());

            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionstring = file.ReadValue("server", "conn");
            //string connectionString = @"Data Source=FOX-PC\SQLEXPRESS;Initial Catalog=Project;Integrated Security=True";
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from BAOHANH where IDbaohanh = @id";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@id", id);

            conn.Open();
            SqlDataReader r = cmd.ExecuteReader();
            while (r.Read())
            {
                namebh.Text = r["tenkhachhang"].ToString();
                sodtbh.Text = r["sodt"].ToString();
                seribh.Text = r["serinumber"].ToString();
                tinhtrangbh.Text = r["tinhtrang"].ToString();
            }
            r.Close();
            conn.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Serial_Number f = new Serial_Number();
            f.ShowDialog();
        }  
    }
}
